package model

import (
	"gorm.io/gorm"
	"wechat/handle/response"
)

type Friend struct {
	gorm.Model
	UserId   int    `gorm:"type:int" json:"userId"`
	FriendId int    `gorm:"type:int" json:"friendId"`
	Content  string `gorm:"type:varchar(30)" json:"content"`
	Status   string `gorm:"type:varchar(10)" json:"status"`
}

// 添加好友
func (f *Friend) AddFriend(db *gorm.DB) error {
	return db.Debug().Create(&f).Error
}

// 验证是否是好友
func (f *Friend) VeryFriend(db *gorm.DB, userId int, friendId int) error {
	return db.Debug().Where("user_id =? and friend_id=?", userId, friendId).First(&f).Error
}

// 好友列表
func (f *Friend) ListFriend(db *gorm.DB, userId int) (list []*response.ListFriend, err error) {
	err = db.Debug().Model(&f).Select("users. *", "users.id as FriendId", "users.phone as Phone", "users.username as Username", "users.image as Image", "users.created_at as Created").
		Joins("left join users on users.id = friends.friend_id").
		Where("friends.user_id=?", userId).Find(&list).Error
	return list, err
}
